var colNames = null;
var colModel = null;
var caption = null;
var groupHeaders = null;
var flag = false;
var jqGridFlag = true;
$(function() {
	$.ajax({
		type : "POST",
		url : baseURL + "apportionment/initialization",
		success : function(r) {
			if (r.code === 0) {
				$("#dateMonthS").val(r.dateMonthS);
				$("#dateMonthE").val(r.dateMonthE);
				$("#dateYearS").val(r.dateYearS);
				$("#dateYearE").val(r.dateYearE);
				colNames = r.colNames;
				colModel = r.colModel;
				caption = r.caption;
				groupHeaders = r.groupHeaders;
				firstLoad();
				setGroupHeaders();
			} else {
				alert(r.msg);
			}
		}
	});
	$('#yearMonth').change(function() {
		flag = true;
	});
	$('#dateMonthS').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				$('#dateMonthS').val(year + '-' + month);
				vm.q.dateMonthS = year + '-' + month;
				$(this).datepicker('close');
				flag = true;
			});
	$('#dateMonthE').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				var date = new Date(event.date);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				if (month < 10) {
					month = '0' + month;
				}
				$('#dateMonthE').val(year + '-' + month);
				vm.q.dateMonthE = year + '-' + month;
				$(this).datepicker('close');
				flag = true;
			});
	$('#dateYearS').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				var date = new Date(event.date);
				var year = date.getFullYear();
				$('#dateYearS').val(year);
				vm.q.dateMonthS = year;
				$(this).datepicker('close');
				flag = true;
			});
	$('#dateYearE').datepicker().on('changeDate.datepicker.amui',
			function(event) {
				var date = new Date(event.date);
				var year = date.getFullYear();
				$('#dateYearE').val(year);
				vm.q.dateMonthE = year;
				$(this).datepicker('close');
				flag = true;
			});
	$('.am-selected-list').find('li').click(function() {
		flag = true;
	});
});
function firstLoad() {
	var dateMonthS = $("#dateMonthS").val();
	var dateMonthE = $("#dateMonthE").val();
	$("#jqGrid").jqGrid(
			{
				url : baseURL + 'apportionment/list',
				datatype : "json",
				postData : {
					'yearMonth' : vm.q.yearMonth,
					'dateMonthS' : dateMonthS,
					'dateMonthE' : dateMonthE
				},
				colNames : colNames,
				colModel : colModel,
				viewrecords : true,
				height : 385,
				rowNum : 10,
				rowList : [ 10, 30, 50 ],
				rownumbers : true,
				rownumWidth : 25,
				autowidth : true,
				shrinkToFit : false,
				footerrow : true,
				caption : caption,
				sortable : false,
				pager : "#jqGridPager",
				jsonReader : {
					root : "page.list",
					page : "page.currPage",
					total : "page.totalPage",
					records : "page.totalCount"
				},
				prmNames : {
					page : "page",
					rows : "limit",
					order : "order"
				},
				gridComplete : function() {
					// jqGrid底部合计
					var rowNum = parseInt($(this).getGridParam("records"), 10);
					if (rowNum > 0) {
						var jz = {};
						$(".ui-jqgrid-sdiv").show();
						for (var i = 4; i < colModel.length; i++) {
							jz[colModel[i].name] = $(this).getCol(
									colModel[i].name, false, "sum").toFixed(2);
						}
						$(this).footerData("set", jz); // 将合计值显示出来
					} else {
						$(".ui-jqgrid-sdiv").hide();
					}
					if (jqGridFlag) {
						// 冻结列
						$("#jqGrid").jqGrid('setFrozenColumns');
						airbus();
						frozenDislocation();
						jqGridFlag = false;
					}
					setTimeout(function() {
						$('.frozen-bdiv.ui-jqgrid-bdiv').height(
								$('.frozen-bdiv.ui-jqgrid-bdiv').height() - 1)
					}, 100);
				}
			});
};
function reLoad() {
	$("#jqGridTable").empty().html(
			'<table id="jqGrid"></table><div id="jqGridPager"></div>');
	var dateMonthS = $("#dateMonthS").val();
	var dateMonthE = $("#dateMonthE").val();
	var dateYearS = $("#dateYearS").val();
	var dateYearE = $("#dateYearE").val();
	var items = $(".am-selected-status.am-fl").text();
	$("#jqGrid").jqGrid(
			{
				url : baseURL + 'apportionment/list',
				datatype : "json",
				postData : {
					'yearMonth' : vm.q.yearMonth,
					'dateMonthS' : dateMonthS,
					'dateMonthE' : dateMonthE,
					'dateYearS' : dateYearS,
					'dateYearE' : dateYearE,
					'buildIds' : vm.q.buildIds,
					'assetIds' : vm.q.assetIds,
					'renter' : vm.q.renter,
					'contractNo' : vm.q.contractNo,
					'items' : items
				},
				colNames : colNames,
				colModel : colModel,
				viewrecords : true,
				height : 385,
				rowNum : 10,
				rowList : [ 10, 30, 50 ],
				rownumbers : true,
				rownumWidth : 25,
				autowidth : true,
				shrinkToFit : false,
				footerrow : true,
				caption : caption,
				sortable : false,
				pager : "#jqGridPager",
				jsonReader : {
					root : "page.list",
					page : "page.currPage",
					total : "page.totalPage",
					records : "page.totalCount"
				},
				prmNames : {
					page : "page",
					rows : "limit",
					order : "order"
				},
				gridComplete : function() {
					// jqGrid底部合计
					var rowNum = parseInt($(this).getGridParam("records"), 10);
					if (rowNum > 0) {
						var jz = {};
						$(".ui-jqgrid-sdiv").show();
						for (var i = 4; i < colModel.length; i++) {
							jz[colModel[i].name] = $(this).getCol(
									colModel[i].name, false, "sum").toFixed(2);
						}
						$(this).footerData("set", jz); // 将合计值显示出来
					} else {
						$(".ui-jqgrid-sdiv").hide();
					}
					if (jqGridFlag) {
						// 冻结列
						$("#jqGrid").jqGrid('setFrozenColumns');
						airbus();
						frozenDislocation();
						jqGridFlag = false;
					}
					setTimeout(function() {
						$('.frozen-bdiv.ui-jqgrid-bdiv').height(
								$('.frozen-bdiv.ui-jqgrid-bdiv').height() - 1)
					}, 100);
				}
			});
};
function setGroupHeaders() {
	$("#jqGrid").jqGrid('setGroupHeaders', {
		useColSpanStyle : true,
		groupHeaders : groupHeaders
	});
};
function airbus() {
	$(".ui-jqgrid-sdiv").find("td[aria-describedby='jqGrid_buildIds']").empty()
			.text("合计");
	$(".frozen-sdiv.ui-jqgrid-sdiv").find(
			"td[aria-describedby='jqGrid_buildIds']").empty().text("合计");
};
function frozenDislocation() {
	// 冻结错位解决
	var bdivTop = -1;
	var sdivTop = -1;
	// opera 不需要hack
	if (navigator.userAgent.indexOf("Opera") >= 0) {
		divTop = 0;
		bdivTop = 0;
	}
	$('.frozen-bdiv.ui-jqgrid-bdiv').css({
		top : $('.frozen-bdiv.ui-jqgrid-bdiv').position().top + bdivTop
	});
	$('.frozen-sdiv.ui-jqgrid-sdiv').css({
		top : $('.frozen-sdiv.ui-jqgrid-sdiv').position().top + sdivTop
	});
};
var vm = new Vue({
	el : '#rrapp',
	data : {
		showList : true,
		q : {
			yearMonth : '1',
		}
	},
	methods : {
		exportExcel : function() {
			vm.q.dateMonthS = $('#dateMonthS').val();
			vm.q.dateMonthE = $('#dateMonthE').val();
			vm.q.dateYearS = $('#dateYearS').val();
			vm.q.dateYearE = $('#dateYearE').val();
			vm.q.items = $(".am-selected-status.am-fl").text();
			var url = "apportionment/export?yearMonth=" + vm.q.yearMonth
					+ "&dateMonthS=" + vm.q.dateMonthS + "&dateMonthE="
					+ vm.q.dateMonthE + "&dateYearS=" + vm.q.dateYearS
					+ "&dateYearE=" + vm.q.dateYearE;
			if (vm.q.buildIds) {
				url += "&buildIds=" + vm.q.buildIds;
			}
			if (vm.q.assetIds) {
				url += "&assetIds=" + vm.q.assetIds;
			}
			if (vm.q.renter) {
				url += "&renter=" + vm.q.renter;
			}
			if (vm.q.contractNo) {
				url += "&contractNo=" + vm.q.contractNo;
			}
			url += "&items=" + vm.q.items + "&token=" + token;
			location.href = baseURL + url;
		},
		query : function() {
			vm.reload();
		},
		reload : function(event) {
			vm.q.dateMonthS = $("#dateMonthS").val();
			vm.q.dateMonthE = $("#dateMonthE").val();
			vm.q.dateYearS = $("#dateYearS").val();
			vm.q.dateYearE = $("#dateYearE").val();
			vm.q.items = $(".am-selected-status.am-fl").text();
			if (flag) {
				// 改变jqgrid表头字段
				$.ajax({
					type : "POST",
					url : baseURL + "apportionment/changeJqGrid",
					data : JSON.stringify(vm.q),
					success : function(r) {
						if (r.code === 0) {
							colNames = r.colNames;
							colModel = r.colModel;
							caption = r.caption;
							groupHeaders = r.groupHeaders;
							jqGridFlag = true;
							reLoad();
							setGroupHeaders();
						} else {
							alert(r.msg);
						}
					}
				});
				flag = false;
			} else {
				var page = $("#jqGrid").jqGrid('getGridParam', 'page');
				$("#jqGrid").jqGrid('setGridParam', {
					postData : {
						'yearMonth' : vm.q.yearMonth,
						'dateMonthS' : vm.q.dateMonthS,
						'dateMonthE' : vm.q.dateMonthE,
						'dateYearS' : vm.q.dateYearS,
						'dateYearE' : vm.q.dateYearE,
						'buildIds' : vm.q.buildIds,
						'assetIds' : vm.q.assetIds,
						'renter' : vm.q.renter,
						'contractNo' : vm.q.contractNo,
						'items' : vm.q.items
					},
					page : page
				}).trigger("reloadGrid");
			}
		}
	}
});